#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
RUNPATH=`dirname $0`

case $1 in

start)
	echo -n "Start ldchknet ..."
	USER=`whoami`
	if [ "$USER" != "root" ] ; then
		echo "Sorry!,you is not root!!"
		exit 1
	fi
	touch $RUNPATH/config/config.dat $RUNPATH/config/srvip.dat $RUNPATH/config/srvport.dat $RUNPATH/config/telnum.dat $RUNPATH/config/.htpasswd
	chmod 666 $RUNPATH/config/config.dat $RUNPATH/config/srvip.dat $RUNPATH/config/srvport.dat $RUNPATH/config/telnum.dat $RUNPATH/config/.htpasswd
	$RUNPATH/shell/checkserverip & > /dev/null 2>&1
	$RUNPATH/shell/checkserverport & > /dev/null 2>&1
	sleep 1
	IPPID=`pgrep checkserverip`
	PORTPID=`pgrep checkserverport`
	if [ "$IPPID" != "" ] && [ "$PORTPID" != "" ] ; then
		echo "OK"
	else
		echo "Fail!!"
		exit 1
	fi
	;;
stop)
	echo -n "Stop ldchknet..."
	kill -9 `pgrep checkserverip` > /dev/null 2>&1
	kill -9 `pgrep checkserverport` > /dev/null 2>&1
	kill -9 `pgrep chksrvip` > /dev/null 2>&1
	kill -9 `pgrep chksrvport` > /dev/null 2>&1
	echo "OK"
	;;
install)
	echo -n "Install ldchknet ..."
	USER=`whoami`
	if [ "$USER" != "root" ] ; then
		echo "Sorry!,you is not root!!"
		exit 1
	fi
	FPING=`which fping`
	if [ "$FPING" = "" ]; then
		echo "Please install fping!!"
		exit 1
	fi
	NMAP=`which nmap`
	if [ "$NMAP" = "" ]; then
		echo "Please install nmap!!"
		exit 1
	fi
	PLAYSOUND=`which mpg123`
	if [ "$PLAYSOUND" = "" ]; then
		echo "Please install mpg123!!"
		exit 1
	fi
	SENDMAIL=`which sendmail`
	if [ "$SENDMAIL" = "" ]; then
		echo "Please install sendmail (qmail,postfix,sendmail)!!"
		exit 1
	fi
	ICENETPATH=`pwd`
	OLDICENETPATH=`cat $RUNPATH/config/config.dat |grep 'ICENETPATH='`
	OLDFPING=`cat $RUNPATH/config/config.dat |grep 'FPING='`
	OLDNMAP=`cat $RUNPATH/config/config.dat |grep 'NMAP='`
	OLDPLAYSOUND=`cat $RUNPATH/config/config.dat |grep 'PLAYSOUND='`
	OLDSENDMAIL=`cat $RUNPATH/config/config.dat |grep 'SENDMAIL='`
	OLDMRTG=`cat $RUNPATH/config/config.dat |grep 'MRTG='`
	sed -i  "s@$OLDICENETPATH@ICENETPATH=$ICENETPATH@g" $RUNPATH/config/config.dat
	sed -i  "s@$OLDFPING@FPING=$FPING@g" $RUNPATH/config/config.dat
	sed -i  "s@$OLDNMAP@NMAP=$NMAP@g" $RUNPATH/config/config.dat
	sed -i  "s@$OLDPLAYSOUND@PLAYSOUND=$PLAYSOUND@g" $RUNPATH/config/config.dat
	sed -i  "s@$OLDSENDMAIL@SENDMAIL=$SENDMAIL@g" $RUNPATH/config/config.dat
	MRTG=`which mrtg`
	if [ "$MRTG" != "" ]; then
		echo "*/5 * * * * sh $ICENETPATH/shell/mrtg.sh" >> /var/spool/cron/crontabs/root
		sed -i  "s@$OLDMRTG@MRTG=$MRTG@g" $RUNPATH/config/config.dat
	fi
	cp $RUNPATH/config/config.dat $RUNPATH/config/config.dat.default
	chmod 666 $RUNPATH/config/*
	chmod 666 $RUNPATH/config/.htpasswd
	chmod 644 $RUNPATH/config/config.dat.default
	OLDADMINPASS=`cat $RUNPATH/html/admin/.htaccess |grep AuthUserFile`
	ADMINPASS=`pwd`/config/.htpasswd
	sed -i "s@$OLDADMINPASS@AuthUserFile\ $ADMINPASS@g" $RUNPATH/html/admin/.htaccess
	echo "OK"
	;;
*)
	cat << EOF
$0 {start|stop|install}
EOF
	;;
esac
exit 0
